"""
Case Type   : 服务端工具
Case Name   : 导出和-t匹配和-T不匹配的数据
Create At   : 2021/1/8
Owner       : opentestcase002
Description :
    1.创建数据库
    2.创建数据
    3.导出和-t匹配和-T不匹配的数据
    4.连接数据库，清理环境
Expect      :
    1.创建数据库成功
    2.创建数据成功
    3.导出成功
    4.清理环境成功
History     :
    modified by @kyleze at 2024/5/9 代码变更，优化断言
    modify by opentestcase002 20220811:研发代码变更，优化断言
    modify by opentestcase002 20221130:研发代码变更，优化断言
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('dbuser')
        self.db_name = 'db_dump_case0046'
        self.t_name1 = 't_dump_case0046_01'
        self.t_name2 = 't_dump_case0046_02'
        self.t_name3 = 't_dump_case0046_03'
        self.v_name1 = 'v_dump_case0046_01'
        self.v_name2 = 'v_dump_case0046_02'
        self.v_name3 = 'v_dump_case0046_03'
        self.constant = Constant()

    def test_server_tools(self):
        text = '-----step1:连接数据库并创建数据库,expect:成功-----'
        self.log.info(text)
        sql_cmd1 = f'''drop database if exists {self.db_name};
            create database {self.db_name};
                   '''
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.log.info(msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, msg1,
                      '执行失败:' + text)

        text = '-----step2:在创建好的数据库中创建表,插入数据并创建视图,expect:成功-----'
        self.log.info(text)
        sql_cmd2 = f''' 
            drop table if  exists {self.t_name1};
            drop table if  exists {self.t_name2};
            drop table if  exists {self.t_name3};
            drop view if  exists {self.v_name1};
            drop view if  exists {self.v_name2};
            drop view if  exists {self.v_name3};
            create table {self.t_name1} (id int);
            insert into {self.t_name1} values(1),(2),(3);
            create table {self.t_name2} (id int);
            insert into {self.t_name2} values(8),(2),(5);
            create table {self.t_name3} (id int);
            insert into {self.t_name3} values(9),(6),(3);
            create view {self.v_name1} as select * from {self.t_name1};
            create view {self.v_name2} as select * from {self.t_name2};
            create view {self.v_name3} as select * from {self.t_name3};
                '''
        msg2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(msg2)
        self.assertEqual(3, msg2.count('INSERT'), '执行失败:' + text)
        self.assertEqual(3, msg2.count('CREATE TABLE'), '执行失败:' + text)
        self.assertEqual(3, msg2.count('CREATE VIEW'), '执行失败:' + text)

        text = '-----step3:导出和-t匹配和-T不匹配的数据,expect:成功-----'
        self.log.info(text)
        excute_cmd3 = f'''source {macro.DB_ENV_PATH};
         gs_dump -p {self.dbuser_node.db_port} {self.db_name} -F p \
         -T {self.v_name1} -t {self.t_name1} \
         -t {self.v_name2} -t {self.t_name3} -t {self.v_name3};
                      '''
        self.log.info(excute_cmd3)
        msg3 = self.dbuser_node.sh(excute_cmd3).result()
        self.log.info(msg3)
        self.assertIn(f'CREATE TABLE {self.t_name1}', msg3)
        self.assertIn(f'COPY public.{self.t_name1}', msg3)
        self.assertIn(f'CREATE TABLE {self.t_name3}', msg3)
        self.assertIn(f'COPY public.{self.t_name3}', msg3)
        self.assertIn(self.constant.GS_DUMP_SUCCESS_MSG, msg3, '执行失败:' + text)

    def tearDown(self):
        text = '-----step4:清理环境,expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f'''drop database if exists {self.db_name};'''
        msg4 = self.sh_primy.execut_db_sql(sql_cmd4)
        self.log.info(msg4)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, msg4,
                      '执行失败:' + text)
        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)
